package com.java.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.java.model.Order;
import com.java.model.PageBean;
import com.java.model.User;
import com.java.service.OrderService;
import com.java.service.UserService;
import com.java.util.AppConfig;
import com.java.util.PageUtil;
import com.java.util.ResponseUtil;
import com.java.util.StringUtil;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/order")
public class OrderController {

	@Autowired
	private OrderService orderService;
	
	@Autowired
	private UserService userService;

	@RequestMapping("/list")
	public ModelAndView list(@RequestParam(value = "page", required = false) String page, Order s_order,
			HttpServletRequest request) {
		ModelAndView mav = new ModelAndView();
		HttpSession session = request.getSession();
		System.out.println(page);
		if (StringUtil.isEmpty(page)) {
			page = "1";
			session.setAttribute("s_order", s_order);
			System.out.println(s_order.getId());
		} else {
			s_order = (Order) session.getAttribute("s_order");
		}
		// 获取当前用户
		User currentUser = (User) session.getAttribute("currentUser");
		s_order.setUserId(currentUser.getId());

		PageBean pageBean = new PageBean(Integer.parseInt(page), AppConfig.pageSize);
		List<Order> orderList = orderService.find(pageBean, s_order);
		int total = orderService.count(s_order);
		String pageCode = PageUtil.getPagation(request.getContextPath() + "/order/list.do", total,
				Integer.parseInt(page), AppConfig.pageSize);
		mav.addObject("pageCode", pageCode);
		mav.addObject("modeName", "我的快递");
		mav.addObject("orderList", orderList);
		mav.addObject("mainPage", "order/list.jsp");
		mav.setViewName("main");
		return mav;
	}
	@RequestMapping("/listAll")
	public ModelAndView listAll(@RequestParam(value = "page", required = false) String page, Order s_order,
			HttpServletRequest request) {
		ModelAndView mav = new ModelAndView();
		HttpSession session = request.getSession();
		System.out.println(page);
		if (StringUtil.isEmpty(page)) {
			page = "1";
			session.setAttribute("s_order", s_order);
			System.out.println(s_order.getId());
		} else {
			s_order = (Order) session.getAttribute("s_order");
		}
		PageBean pageBean = new PageBean(Integer.parseInt(page), AppConfig.pageSize);
		List<Order> orderList = orderService.find(pageBean, s_order);
		int total = orderService.count(s_order);
		String pageCode = PageUtil.getPagation(request.getContextPath() + "/order/listAll.do", total,
				Integer.parseInt(page), AppConfig.pageSize);
		mav.addObject("pageCode", pageCode);
		mav.addObject("modeName", "快递信息管理");
		mav.addObject("orderList", orderList);
		mav.addObject("mainPage", "order/list.jsp");
		mav.setViewName("main");
		return mav;
	}

	@RequestMapping("/preSave")
	public ModelAndView preSave(@RequestParam(value = "id", required = false) String id) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("mainPage", "order/save.jsp");
		mav.addObject("modeName", "快递订单管理");
		mav.setViewName("main");
		if (StringUtil.isNotEmpty(id)) {
			mav.addObject("actionName", "快递订单修改");
			Order order = orderService.loadById(Integer.parseInt(id));
			mav.addObject("order", order);
		} else {
			mav.addObject("actionName", "快递订单添加");
		}
		return mav;
	}

	@RequestMapping("/save")
	public String save(Order order, HttpSession session) {
		if (order.getId() == null) {
//			System.out.println("add:order:"+order.toString());
			User cuser = (User) session.getAttribute("currentUser");
//			System.out.println(cuser.toString());
			order.setCompanyId(Integer.valueOf(cuser.getCompanyId()));
//			System.out.println("add:order:"+order.toString());
//			User user = userService.loadById(order.getUserId());
//			order.setUserId(user.getId());
			orderService.add(order);
		} else {
			System.out.println("update:order:"+order.toString());
			orderService.update(order);
		}
		return "redirect:/order/listAll.do";
	}

	@RequestMapping("/delete")
	public void delete(@RequestParam(value = "id") String id, HttpServletResponse response) throws Exception {
		JSONObject result = new JSONObject();
		orderService.delete(Integer.parseInt(id));
		result.put("success", true);
		ResponseUtil.write(result, response);
	}
	
	
	/**
	 * 若存在则返回“1”，若不存在则返回“0”
	 * 
	 * @param orderId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/ajaxValidateOrderId", method = RequestMethod.POST)
	public String validateLastName(@RequestParam(value = "orderId", required = true) Integer orderId) {
		Order order = orderService.loadById(Integer.valueOf(orderId));
		boolean isExist = false;
		if (order.getId() == orderId){
			isExist = true;
		}
		if (!isExist) {
			return "0";
		} else {
			return "1";
		}
	}
	
	
}
